public class MyLinkedList {
    static class ListNode{
        int val;
        ListNode prev;
        ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }
    ListNode head;
    ListNode end;

    public void addFirst(int data){
        if(head == null){
            head.val = data;
            head.next = null;
            head.prev = null;
            end = head;
        }
        ListNode node = new ListNode(data);
        node.next = head;
        head.prev = node;
        head = node;
    }

    public void addLast(int data){
        if(head == null){
            head.val = data;
            head.next = null;
            head.prev = null;
            end = head;
        }
        ListNode node = new ListNode(data);
        node.prev = end;
        end.next = node;
        end = node;
    }

    public boolean addIndex(int index,int data){
        int ret = size();
        ListNode cur = head;
        if(index > ret){
            System.out.println("插入位置不合法，请重新输入!");
            return false;
        }
        if(index == ret){
            addLast(data);
        }
        if(index == 0){
            addFirst(data);
        }
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        ListNode node = new ListNode(data);
        node.prev = cur;
        node.next = cur.next;
        cur.next = node;
        return false;
    }

    public int size(){
        ListNode cur = head;
        int size = 0;
        if(head == null){
            return size;
        }
        while(cur != null){
            cur = cur.next;
            size++;
        }
        return size;
    }
    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.println(cur.val+" ");
            cur = cur.next;
        }
        System.out.println("============");
    }
    public void clear(){
        ListNode cur = head;
        while(cur != null){
            cur = cur.next;
            ListNode curPrev = cur.prev;
            curPrev.next = null;
            curPrev.prev = null;
        }
        head = null;
        end = null;
    }

    public boolean contains(int key){
        if(head == null){
            System.out.println("链表为空！");
            return false;
        }
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }else {
                cur = cur.next;
            }
        }
        return false;
    }
    public void remove(int key){
        if(head == null){
            System.out.println("链表为空！");
            return;
        }
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = head.next;
                    if(head != null){
                        head.prev = null;
                    }else {
                        end = null;
                    }
                }else{
                    if(cur.next != null){
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }else {
                        cur.prev.next = cur.next;
                        end = end.prev;
                    }
                }
                return;
            }else {
            cur = cur.next;
        }
        }
    }

    public void removeAllKey(int key) {
        if (head == null) {
            System.out.println("链表为空！");
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    } else {
                        end = null;
                    }
                } else {
                    if (cur.next != null) {
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    } else {
                        cur.prev.next = cur.next;
                        end = end.prev;
                    }
                }
                }else{
                cur = cur.next;
            }
        }
    }
    public boolean hasCycle(ListNode head) {
        if(head == null){
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        if(fast == null || fast.next == null){
            return false;
        }
        return true;
    }

    public ListNode detectCycle(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        if(fast == null || fast.next == null){
            return null;
        }
        fast = head;
        while(fast != slow){
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}
